﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoodsInvoicing.Views.View;
using GoodsInvoicing.Model.UserRole;
using Spring.Context;
using GoodsInvoicing.Presentions.Presenter;
using GoodsInvoicing.Presentions.PresenterImpl;
using Spring.Context.Support;
using GoodsInvoicing.BLL.Service;

namespace GoodsInvoicing.Views.ViewImpl
{
    public partial class UserSaveForm : Form, IUserSaveForm
    {
        private string userName = "", loginName = "", loginPWD = "", telephone = "";
        private Int32 userID = 0;

        private IApplicationContext applicationContext;
        private IUserSaveFormPresenter presenter;

        public UserSaveForm()
        {
            InitializeComponent();

            applicationContext = ContextRegistry.GetContext();
            presenter = new UserSaveFormPresenter(this, applicationContext["GI_UserService"] as IGI_UserService);
            presenter.InitializeView();
        }

        public UserSaveForm(Model.UserRole.GI_User user)
        {
            InitializeComponent();

            InitializeForm(user);

            applicationContext = ContextRegistry.GetContext();
            presenter = new UserSaveFormPresenter(this, applicationContext["GI_UserService"] as IGI_UserService);
            presenter.InitializeView();
        }

        private void InitializeForm(Model.UserRole.GI_User user)
        {
            this.tbUserID.Text = user.UserID.ToString();
            this.tbUserName.Text = user.UserName;
            this.tbLoginName.Text = user.LoginName;
            this.tbLoginPWD.Text = user.LoginPWD;
            this.tbTelephone.Text = user.Telephone;
        }

        #region 属性
        public event EventHandler<EventArgs> UserAddButtonClick;
        public event EventHandler<EventArgs> UserSaveButtonClick;

        public int UserID
        {
            get
            {
                return userID;
            }
            set
            {
                this.userID = value;
            }
        }

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                this.userName = value;
            }
        }

        public string LoginName
        {
            get
            {
                return loginName;
            }
            set
            {
                this.loginName = value;
            }
        }

        public string LoginPWD
        {
            get
            {
                return loginPWD;
            }
            set
            {
                this.loginPWD = value;
            }
        }

        public string Telephone
        {
            get
            {
                return telephone;
            }
            set
            {
                this.telephone = value;
            }
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            GI_User user = new GI_User();

            if (ValidateForm())
            {
                ParseFormToUser();

                if (this.tbUserID.Text.Equals("0"))
                {
                    this.UserAddButtonClick(sender, e);
                }
                else
                {
                    this.UserSaveButtonClick(sender, e);
                }

                (this.MdiParent.MdiChildren[0] as UserListForm).UserListForm_Load(sender, e); 
                this.Close(); 

            }

        }

        /// <summary>
        /// 将Form中的输入框转换为User对象
        /// </summary>
        private void ParseFormToUser()
        {
            this.LoginName = this.tbLoginName.Text.Trim();
            this.LoginPWD = this.tbLoginPWD.Text.Trim();
            this.UserID = Int32.Parse(this.tbUserID.Text.Trim());
            this.UserName = this.tbUserName.Text.Trim();
            this.Telephone = this.tbTelephone.Text.Trim();
        }

        /// <summary>
        /// Form验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateForm()
        {
            epUserSave.Clear();
            if (this.tbLoginName.Text.Length == 0)
            {
                epUserSave.SetError(this.tbLoginName, "登录名不能为空！");
                return false;
            }
            if (this.tbLoginPWD.Text.Length == 0)
            {
                epUserSave.SetError(this.tbLoginPWD, "登录密码不能为空！");
                return false;
            }
            if (this.tbUserName.Text.Length == 0)
            {
                epUserSave.SetError(this.tbUserName, "用户姓名不能为空");
            }
            return true;
        }


    }
}
